import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;

public class DomEditNode {
	public DomEditNode() {
		User user = new User();
		user.setUserId("103");
		// update username
		user.setUserName("dsdfsfsdfsdfsdf");
		editUser(user);

	}

	public static void main(String[] args) {
		new DomEditNode();
	}

	public void editUser(User user) {
		File file = new File("users.xml");

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc;
		try {
//			doc = factory.newDocumentBuilder().parse(new InputSource(new ByteArrayInputStream("users.xml".getBytes("utf-8"))));
			doc = factory.newDocumentBuilder().parse(file);
			Node documentElem = doc.getDocumentElement();

			NodeList nodeList = documentElem.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node childNode = nodeList.item(i);
				if (childNode.getNodeName() == "user") {
					System.out.println(childNode.getTextContent().trim());
					NodeList level2 = childNode.getChildNodes();
					for (int j = 0; j < level2.getLength(); j++) {
						Node child = level2.item(j);

						if ("id".equals(child.getNodeName())
								&& user.getUserId().equalsIgnoreCase(child
										.getTextContent().trim())) {
							Node oldNode = child.getParentNode();
							Element newNode = doc.createElement("user");

							Element idNode = doc.createElement("id");
							newNode.appendChild(idNode);
							Text userId = doc.createTextNode(user.getUserId());
							idNode.appendChild(userId);

							Element nameNode = doc.createElement("name");
							newNode.appendChild(nameNode);
							Text userName = doc.createTextNode(user
									.getUserName());
							nameNode.appendChild(userName);

							Element root = doc.getDocumentElement();
							root.replaceChild(newNode, oldNode);

							saveToFile(doc);
						}
					}

				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}

	}

	private void saveToFile(Document doc) {
		// save
		// set up a transformer
		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans;
		try {
			trans = transfac.newTransformer();

			// create string from xml tree
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			String xmlString = sw.toString();
			// write to file
			FileWriter out= new FileWriter("output.xml");
			out.write(xmlString);
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void changeContent(Document doc, User user) {

	}
}
